package com.spyz.zk_api;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class zkCilent {
    private  String connectString="master:2181,slave1:2181,slave2:2181";
    private  int sessionTimeout=2000;
    private ZooKeeper zkCilent;

    @Before
    public  void init() throws IOException {
        zkCilent = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
            }
        });
    }

    public void create() throws KeeperException, InterruptedException {
        String path="/test";
        String data="hello";
        String nodeCreate = zkCilent.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    @Test
    public void getChildren() throws Exception {
        List<String> children = zkCilent.getChildren("/", true);


        for (String child : children) {
            System.out.println(child);
        }
    }

    @Test
    public void exists() throws Exception {
        Stat sta = zkCilent.exists("/test", false);

        System.out.println(sta==null ? "不存在":"存在");
    }
}
